﻿package mortal.game.view.copy.singleTowerCopy2.panel
{
    import Message.DB.Tables.*;
    import Message.Public.*;
    import com.mui.controls.*;
    import extend.language.*;
    import flash.events.*;
    import mortal.common.global.*;
    import mortal.common.net.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.scene3D.ai.*;
    import mortal.game.scene3D.player.info.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.display.*;
    import mortal.game.view.common.item.*;
    import mortal.game.view.copy.*;
    import mortal.game.view.copy.GroupTowerCopy.data.*;
    import mortal.game.view.copy.singleTowerCopy2.data.*;
    import mortal.game.view.guildNew.*;
    import mortal.mvc.core.*;

    public class SingleTowerInfoPanel extends CopyInfoPanelBase
    {
        protected var _bgM:GImageBitmap;
        protected var _guanTxt:GTextFiled;
        protected var _copyRwList:CellsGridSprite;
        protected var _time:BitmapNumberText;
        protected var _boss:GImageBitmap;
        protected var _tip1:GTextFiled;
        protected var _tip2:GTextFiled;
        protected var _tip3:GTextFiled;
        protected var _rentBtn:GLoadingButton;
        protected var _autoFBtn:GLoadingButton;
        protected var _exitBtn:GLoadingButton;
        protected var _proBar:BaseProgressBar;
        protected var _secView:SecTimerView;
        private var _entityInfo:EntityInfo;
        private var _info:STowerCopy4Msg;
        private static var _instance:SingleTowerInfoPanel;

        public function SingleTowerInfoPanel()
        {
            super(ImagesConst.copyInfoPanelTitle_groupTower, 250, 535);
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this._secView = new SecTimerView();
            this._secView.addEventListener(EventName.SecViewTimeChange, this.onTimeChangeHd);
            this._bgM = UIFactory.gImageBitmap(ImagesConst.SingleTowerCopyInfoBg, 0, 0, _bodySprite);
            this._guanTxt = UIFactory.gTextField("10/20", 139, 194, 100, 25, _bodySprite, GlobalStyle.textFormatHuang2.setSize(15));
            this._copyRwList = UIFactory.getUICompoment(CellsGridSprite);
            this._copyRwList.colNum = 4;
            this._copyRwList.vCellWid = 43;
            this._copyRwList.hCellWid = 100;
            this._copyRwList.genCellFun = UIFactory.getUICompoment;
            this._copyRwList.genCells(4, BaseItem);
            this._copyRwList.x = 66;
            this._copyRwList.y = 232;
            _bodySprite.addChild(this._copyRwList);
            this._time = UIFactory.bitmapNumberText(132, 315, "ChallengeTimerNum.png", 35, 48, 0, _bodySprite, 10, 1);
            this._time.text = "15(00";
            this._boss = UIFactory.gImageBitmap("AvatarBig_2_0.png", 103, 83, _bodySprite);
            this._tip1 = UIFactory.gTextField("" + Language.getString(70960), 87, 367, 100, 20, _bodySprite, GlobalStyle.textFormatPutong.setSize(13));
            this._tip2 = UIFactory.gTextField("" + Language.getString(70961), 27, 425, 100, 20, _bodySprite, GlobalStyle.textFormatLv.setBold(true));
            this._tip3 = UIFactory.gTextField("" + Language.getString(70962), 53, 446, 200, 20, _bodySprite, GlobalStyle.textFormatLv.setBold(true));
            this._rentBtn = UIFactory.gLoadingButton(ImagesConst.GuildNewMercenaryRentBtn_upSkin, 100, 397, 90, 45, _bodySprite);
            this._rentBtn.label = "";
            this._autoFBtn = UIFactory.gLoadingButton(ImagesConst.autoFightBtn_upSkin, 24, 485, 103, 38, _bodySprite);
            this._autoFBtn.label = "";
            this._exitBtn = UIFactory.gLoadingButton(ImagesConst.ExitCopyBtn_upSkin, 135, 485, 103, 38, _bodySprite);
            this._exitBtn.label = "";
            this._proBar = UIFactory.getUICompoment(BaseProgressBar, 95, 158, _bodySprite);
            this._proBar.setBg(ImagesConst.PetLifeBg, true, 145, 14, true);
            this._proBar.setProgress(ImagesConst.PetExp, true, 1, 1, 143, 12);
            this._proBar.setLabel(BaseProgressBar.ProgressBarTextNumber, 50, -4, 50, 20, GlobalStyle.textFormatHuang);
            this._proBar.setValue(100, 100);
            this._rentBtn.configEventListener(MouseEvent.CLICK, this.onRentHd);
            this._autoFBtn.configEventListener(MouseEvent.CLICK, this.autoFight);
            this._exitBtn.configEventListener(MouseEvent.CLICK, this.onQuickHd);
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            this._bgM = null;
            this._guanTxt = null;
            this._copyRwList = null;
            this._time = null;
            this._boss = null;
            this._tip1 = null;
            this._tip2 = null;
            this._tip3 = null;
            this._rentBtn = null;
            this._autoFBtn = null;
            this._exitBtn = null;
            this._proBar = null;
            this._secView = null;
            if (this._entityInfo)
            {
                this._entityInfo.removeEventListener(EntityInfoEventName.UpdateLife, this.updateLife);
                this._entityInfo = null;
            }
            super.disposeImpl(param1);
            return;
        }// end function

        public function updateInfo(param1:STowerCopy4Msg) : void
        {
            if (isDisposed)
            {
                return;
            }
            this._info = param1;
            if (this._info == null)
            {
                return;
            }
            this.updateGuanTxt();
            if (!GroupTowerCopyUtil.isStarTime)
            {
                this._secView.stop();
            }
            else
            {
                this._secView.setLeftTime(param1.surplusSeconds);
            }
            return;
        }// end function

        public function stopTime() : void
        {
            return;
        }// end function

        protected function onTimeChangeHd(event:DataEvent) : void
        {
            if (this._secView == null)
            {
                return;
            }
            if (this._secView.secTimerViewData == null)
            {
                this._secView.stop();
                return;
            }
            var _loc_2:* = this._secView.secTimerViewData.mm();
            var _loc_3:* = this._secView.secTimerViewData.ss();
            this._time.text = _loc_2 + "(" + _loc_3;
            return;
        }// end function

        public function updateGuanTxt() : void
        {
            if (this._info == null)
            {
                return;
            }
            var _loc_1:* = this._info.level;
            this._guanTxt.text = "" + _loc_1 + "/" + SingleTowerCopyUtil.getTotalLvByCode(this._info.copyCode);
            this.updateCopyInfo(_loc_1);
            return;
        }// end function

        private function updateCopyInfo(param1:int) : void
        {
            var _loc_8:ItemData = null;
            var _loc_9:BaseItem = null;
            if (this._info == null)
            {
                return;
            }
            var _loc_2:* = this._info.copyCode;
            var _loc_3:* = GroupTowerConfig.instance.getInfoByCodeAndLvl(this._info.copyCode, param1);
            if (_loc_3 == null)
            {
                return;
            }
            var _loc_4:* = parseInt(_loc_3.passCondition);
            var _loc_5:* = BossConfig.instance.getInfoByCode(_loc_4);
            this._boss.imgUrl = _loc_5.avatarId + ".png";
            var _loc_6:Array = null;
            if (_loc_6 == null)
            {
                _loc_6 = SingleTowerCopyUtil.getComRwArrByCodeAndLv(this._info.copyCode, param1);
            }
            this._copyRwList.genCells(4, BaseItem);
            var _loc_7:int = 0;
            while (_loc_7 < this._copyRwList.cellArr.length && _loc_7 < _loc_6.length)
            {
                
                _loc_8 = _loc_6[_loc_7];
                _loc_9 = this._copyRwList.cellArr[_loc_7];
                _loc_9.itemData = _loc_8;
                _loc_7++;
            }
            this.getBoss(null);
            CallLater.setCallLater(this.getBoss, 2);
            return;
        }// end function

        private function getBoss(param1:Object) : void
        {
            var _loc_4:TBoss = null;
            if (isDisposed)
            {
                return;
            }
            var _loc_2:* = Cache.instance.entity.getAllEntityInfo(EEntityType._EEntityTypeBoss);
            if (_loc_2 == null || _loc_2[0] == null)
            {
                return;
            }
            var _loc_3:* = _loc_2[0] as EntityInfo;
            if (_loc_3)
            {
                _loc_4 = BossConfig.instance.getInfoByCode(_loc_3.entityInfo.code);
                if (_loc_4 && _loc_4.category == EBossCategory._EBossCategoryBoss)
                {
                    this._entityInfo = _loc_3;
                    this._entityInfo.addEventListener(EntityInfoEventName.UpdateLife, this.updateLife);
                    this.updateLife();
                }
            }
            return;
        }// end function

        private function updateLife(event:Event = null) : void
        {
            if (this._proBar && this._entityInfo && this._entityInfo.entityInfo)
            {
                this._proBar.setValue(this._entityInfo.entityInfo.life, this._entityInfo.entityInfo.maxLife);
            }
            return;
        }// end function

        private function onRentHd(event:MouseEvent) : void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.GuildNewMercenaryOpen, GuildNewModule.Mercenary));
            return;
        }// end function

        private function onQuickHd(event:MouseEvent) : void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.CopyClickQuickBtn));
            return;
        }// end function

        private function autoFight(event:MouseEvent) : void
        {
            AIManager.cancelAll();
            Dispatcher.dispatchEvent(new DataEvent(EventName.AutoFight_A));
            return;
        }// end function

        public static function get instance() : SingleTowerInfoPanel
        {
            if (_instance == null)
            {
                _instance = new SingleTowerInfoPanel;
            }
            return _instance;
        }// end function

        public static function get isView() : Boolean
        {
            return _instance && !_instance.isHide;
        }// end function

    }
}
